import 'package:flutter/material.dart';
import 'package:window_example/tab/custom_tab_controller.dart';
import 'package:window_example/tab/custom_tabs.dart';
import 'package:window_example/util/print_util.dart';

import '../interface/content_page.dart';

class TabContent extends ContentPage {
  @override
  Widget getContent() {
    return const CustomTabWidget();
  }
}

class CustomTabWidget extends StatefulWidget {
  const CustomTabWidget({super.key});

  @override
  State<CustomTabWidget> createState() => _CustomTabWidgetState();
}

class _CustomTabWidgetState extends State<CustomTabWidget> {
  var tabs = <Widget>[
    Text("Tab2"),
    Text("Tab3"),
    Text("Tab4"),
  ];
  static const tabViews = <Widget>[
    TabPage2(),
    TabPage3(),
    TabPage4(),
  ];
  late CustomerTabController controller;

  @override
  void initState() {
    controller = CustomerTabController(tabs.length);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.blueAccent,
            child: CustomerTabBar(tabs, controller),
          ),
        ),
        Expanded(flex: 3, child: CustomTabView(tabViews, controller))
      ],
    );
  }
}

class TabWidget extends StatefulWidget {
  const TabWidget({super.key});

  @override
  State<TabWidget> createState() => _TabWidgetState();
}

class _TabWidgetState extends State<TabWidget> {
  final List _pageList = [
    TabPage(),
    TabPage4(),
  ];

  var currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        initialIndex: 1,
        length: 2,
        child: Scaffold(
          body: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Expanded(child: _pageList[currentIndex]),
              BottomAppBar(
                color: Colors.white,
                shape: const CircularNotchedRectangle(), // 底部导航栏打一个圆形的洞
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    IconButton(
                      icon: const Icon(Icons.home),
                      onPressed: () {
                        currentIndex = 0;
                        setState(() {});
                      },
                    ),
                    SizedBox(), //中间位置空出
                    IconButton(
                      icon: const Icon(Icons.business),
                      onPressed: () {
                        currentIndex = 1;
                        setState(() {});
                      },
                    ),
                  ], //均分底部导航栏横向空间
                ),
              )
            ],
          ),
        ));
  }
}

class TabPage extends StatefulWidget {
  const TabPage({super.key});

  @override
  State<TabPage> createState() => _TabPageState();
}

class _TabPageState extends State<TabPage> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  var tabList = <Widget>[
    Tab(
      icon: Icon(Icons.cloud_outlined),
    ),
    Tab(
      icon: Icon(Icons.beach_access_sharp),
    ),
    Tab(
      icon: Icon(Icons.brightness_5_sharp),
    ),
  ];
  var tabViewList = <Widget>[
    TabPage1(),
    TabPage2(),
    TabPage3(),
  ];

  @override
  void didUpdateWidget(TabPage oldWidget) {
    super.didUpdateWidget(oldWidget);
    debugPrint("_TabPageState didUpdateWidget......");
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(vsync: this, length: tabList.length);
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("_TabPageState build......");
    return Scaffold(
      body: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
              color: Colors.blueAccent,
              child: TabBar(
                controller: _tabController,
                indicatorColor: Colors.purple,
                tabs: tabList,
              )),
          Expanded(
              child: TabBarView(
            controller: _tabController,
            children: tabViewList,
          )),
        ],
      ),
    );
  }
}

class TabPage1 extends StatefulWidget {
  const TabPage1({super.key});

  @override
  State<TabPage1> createState() => _TabPage1State();
}

mixin LifeCycle {}

abstract class BaseState<T extends StatefulWidget> extends State<T> {
  String? mTAG;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    LoggerUtil.d("initState", mTAG);
  }

  Widget getWidget();

  // String getTag();

  @override
  Widget build(BuildContext context) {
    LoggerUtil.d("build", mTAG);
    return getWidget();
  }

  @override
  void didChangeDependencies() {
    LoggerUtil.d("didChangeDependencies", mTAG);
    super.didChangeDependencies();
  }

  @override
  void didUpdateWidget(T oldWidget) {
    LoggerUtil.d("didUpdateWidget", mTAG);
    super.didUpdateWidget(oldWidget);
  }

  @override
  void deactivate() {
    LoggerUtil.d("deactivate", mTAG);
    super.deactivate();
  }

  @override
  void dispose() {
    LoggerUtil.d("dispose", mTAG);
    super.dispose();
  }
}

class _TabPage1State extends State<TabPage1> {
  String? get mTAG => "_TabPage1State";

  @override
  Widget build(BuildContext context) {
    LoggerUtil.d("build", mTAG);
    return const Center(
      child: Text("TabPage1"),
    );
  }
}

class TabPage2 extends StatefulWidget {
  const TabPage2({super.key});

  @override
  State<TabPage2> createState() => _TabPage2State();
}

class _TabPage2State extends BaseState<TabPage2> {
  @override
  // TODO: implement mTAG
  String? get mTAG => "_TabPage2State";

  @override
  Widget getWidget() {
    return const Center(
      child: Text("TabPage2"),
    );
  }
}

class TabPage3 extends StatefulWidget {
  const TabPage3({super.key});

  @override
  State<TabPage3> createState() => _TabPage3State();
}

class _TabPage3State extends BaseState<TabPage3> {
  @override
  // TODO: implement mTAG
  String? get mTAG => "_TabPage3State";

  @override
  Widget getWidget() {
    return const Center(
      child: Text("TabPage3"),
    );
  }
}

class TabPage4 extends StatefulWidget {
  const TabPage4({super.key});

  @override
  State<TabPage4> createState() => _TabPage4State();
}

class _TabPage4State extends BaseState<TabPage4> {
  @override
  // TODO: implement mTAG
  String? get mTAG => "_TabPage4State";

  @override
  Widget getWidget() {
    return const Center(
      // child: Webview(url: "https://www.baidu.com/"),
      child: Text("TabPage4"),
    );
  }
}
